﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminListAboutUsController : Controller
    {
        //
        // GET: /AdminCP/AdminListAboutUs/
        #region "Index"
        public ActionResult Index(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityListAboutUs
                            join q in db.EntityAboutUs on p.IdAboutUs equals q.Id
                            where p.IdAboutUs==id
                            select new AdminListAboutUsModel
                            {
                                Id = p.Id,
                                AboutUsName = q.Name,
                                Name = p.Name,
                                Description = p.Description,
                                Image = p.Image,
                                Body = p.Body,
                                DateCreate = p.DateCreate
                            };
                if (query != null)
                    return View(query.ToList());
                else
                    return View("Error");
            }

        }
        #endregion

        #region "Edit"
        public ActionResult Edit(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityListAboutUs
                            where p.Id == id
                            select new AdminListAboutUsModel
                            {
                                Id = p.Id,
                                IdAboutUs=p.IdAboutUs,
                                Name = p.Name,
                                Description = p.Description,
                                Image = p.Image,
                                Body = p.Body,
                                DateCreate = p.DateCreate
                            };
                if (query != null)
                    return View(query.FirstOrDefault());
                else
                    return View("Error");

            }
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(AdminListAboutUsModel model,HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = db.EntityListAboutUs.FirstOrDefault(p => p.Id == model.Id);
                query.IdAboutUs = model.IdAboutUs;
                query.Name = model.Name;
                query.Description = model.Description;
                query.Body = model.Body;
                query.DateCreate = model.DateCreate;
                if (Image != null && Image.ContentLength > 0)
                {
                    int kb = Image.ContentLength / 1024; //file size kb
                    if (kb >= 2048) // file qua lon
                    {
                        return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                    }
                    //delete file cu
                    if (query.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/aboutus/"), query.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }

                    ImageHelper imgHelper = new ImageHelper();
                    string encodestring = imgHelper.encodeImageFile(Image);

                    if (encodestring == "!")
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                    var path = Path.Combine(Server.MapPath("~/Content/upload/aboutus/"), encodestring);
                    Image.SaveAs(path);

                    query.Image = encodestring;
                }
                db.SubmitChanges();
                return RedirectToAction("Index", new { id = model.IdAboutUs });
            }
        }
        #endregion

        #region "Insert"
        public ActionResult Insert()
        {
            return View();
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(AdminListAboutUsModel model, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityListAboutUs query = new EntityListAboutUs();
                query.IdAboutUs = 1;             
                query.Name = model.Name;
                query.Description = model.Description;
                query.Body = model.Body;
                query.DateCreate = model.DateCreate;
                if (Image != null && Image.ContentLength > 0)
                {
                    int kb = Image.ContentLength / 1024; //file size kb
                    if (kb >= 2048) // file qua lon
                    {
                        return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                    }
                    //delete file cu
                    if (query.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/aboutus/"), query.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }

                    ImageHelper imgHelper = new ImageHelper();
                    string encodestring = imgHelper.encodeImageFile(Image);

                    if (encodestring == "!")
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                    var path = Path.Combine(Server.MapPath("~/Content/upload/aboutus/"), encodestring);
                    Image.SaveAs(path);

                    query.Image = encodestring;
                }
                db.EntityListAboutUs.InsertOnSubmit(query);
                db.SubmitChanges();
                return RedirectToAction("Index", new { id = 1 });
            }
        }
        #endregion

        #region "Delete"
        public string Delete(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityListAboutUs aboutUs = db.EntityListAboutUs.FirstOrDefault(p => p.Id == id);
                if (aboutUs != null)
                {
                    if (aboutUs.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/aboutus/"), aboutUs.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }
                    db.EntityListAboutUs.DeleteOnSubmit(aboutUs);
                    db.SubmitChanges();
                    return "Ok";
                }
                else
                    return "Error";
            }
        }
        #endregion
    }
}
